<template>
  <div id="app">
    <transition :name="transitionName">
      <router-view v-if="isRouterAlive" />
    </transition>
    
  </div>
</template>

<style lang="scss">
html,body{
  overflow: hidden;
}
*{
   -webkit-tap-highlight-color: transparent
}
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  overflow: auto;
}

.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  will-change: transform;
  transition: all 10ms;
  animation: all 0ms;
  position: absolute;
}

.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

</style>
<script>
export default {
  // 依赖注入
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      transitionName: "",
      isRouterAlive: true
    }
  },
  methods: {
    // 解决刷新问题
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      })
    }
  },
  // 监听路由跳转
  // 启动动画
  watch: {
    '$route' (to, from) {
      console.log(to)

      const toIndex = to.meta.index
      const fromIndex = from.meta.index

      this.transitionName = toIndex < fromIndex ? 'slide-right' : 'slide-left'
    }
  }
}
</script>
